App.controller('adDesignController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

	Page.ajaxPostQuiet('customer/list.do', null, function (resp) {
        $scope.customerList = resp.data;
	});
	
    $scope.grid = {
        add : function() {
            $scope.viewFlag = false;
            $scope.adDesign = {};
            $scope.modal.show();
        },
        update: function(row) {
            $scope.viewFlag = false;
            $scope.adDesign = row;
            $scope.adDesign.file = {};
            $scope.adDesign.file.path = row.path;
            $scope.modal.show();
        },
        view : function(row) {
            $scope.viewFlag = true;
            $scope.adDesign = row;
            $scope.adDesign.file = {};
            $scope.adDesign.file.path = row.path;
            $scope.modal.show();
        },
        save : function() {
        	if(!$scope.validateForm.$valid) {
	            $scope.validateForm.doValidate();
	        } else {
                if($scope.adDesign.file != undefined) {
                    $scope.adDesign.path = $scope.adDesign.file.path;
                } else {
                    $scope.adDesign.path = "";
                }
                if($scope.adDesign.status == '1') { //状态为完成的时候需要验证文件上传必填
                    if($scope.adDesign.path == undefined || $scope.adDesign.path == '') {
                        Page.notice("请上传确认稿!");
                        return;
                    }
                }
	        	Page.ajaxPost($scope.adDesign.adId ? 'adDesign/update.do' : 'adDesign/add.do', $scope.adDesign, function() {
	                $scope.grid.query();
	                $scope.grid.cancel();
	            });	        
	        }
        },
        viewSampleImg : function() {
            //$scope.sampleUrlTip.show(event);
        },
        column_defs: [
            {
              target : 3,
              template: function(row) {
                  var customerName = '';
                  $($scope.customerList).each(function(idx,customer){
                      if(customer.customerId == row.customerId){
                          customerName = customer.customerName;
                          return false;
                      }
                  });
                  return customerName;
              }
            },
            {
                target: 6,
                template: function(row) {
                    if(row.path != undefined && row.path != '') {
                        return '<a ed-click="viewSampleImg">预览</a>';
                    }
                    return '<span></span>'
                }
            },
            {
                target : 12,
                buttons : [
                    {
                        name: '编辑',
                        class: 'btn-warning btm-sm',
                        icon: 'fa-edit',
                        visiable: function(row) {
                            return row.status && row.status == '0';
                        },
                        event: 'update'
                    },
                    {
                        name: '删除',
                        class: 'btn-danger',
                        icon: 'fa-close',
                        visiable: function(row) {
                            return row.status && row.status == '0';
                        },
                        event: function(row) {
                            Page.confirm("确定要删除吗？", function() {
                                Page.ajaxPost('adDesign/delete.do', row, function() {
                                    $scope.grid.query();
                                });
                            });
                        }
                    },
                    {
                        name: '查看',
                        class: 'btn-info',
                        icon: 'glyphicon glyphicon-eye-open',
                        visiable: function(row) {
                            return row.status && row.status == '1';
                        },
                        event: 'view'
                    },
                ]
            }
        ]
    };
    
    $scope.removeImg  = function(){
        $scope.adDesign.file = {};
    };


}]);